package com.study.ui.anim

import android.app.ActivityOptions
import android.content.Intent
import com.study.base.BaseActivity
import com.study.ui.R
import kotlinx.android.synthetic.main.activity_activity_anim.*
import org.jetbrains.anko.sdk27.coroutines.onClick

/**
 * <pre>
 *     author : slx
 *     time   : 2020/04/03
 *     desc   : Activity转场动画
 * </pre>
 */
class ActivityAnimActivity : BaseActivity() {
    override fun generateLayoutId() = R.layout.activity_activity_anim

    override fun initView() {
        super.initView()
        useOverridePendingTransition()
        useActivityOptions()
        userShareView()
    }

    //View共享 两个界面相应组件的android:transitionName="shareName"属性必须相同
    private fun userShareView() {
        activityShareBtn.onClick {
            val intent = Intent(this@ActivityAnimActivity, ActivityAnim2Activity::class.java)
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this@ActivityAnimActivity, activityShareBtn, "activityShareBtn").toBundle())
        }
    }

    //MD风格转场动画 ActivityOptions
    private fun useActivityOptions() {
        activityOptionsBtn.onClick {
            val intent = Intent(this@ActivityAnimActivity, ActivityAnim2Activity::class.java)
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this@ActivityAnimActivity).toBundle())
        }
    }

    //使用overridePendingTransition添加动画
    private fun useOverridePendingTransition() {
        pendingAnimBtn.onClick {
            val intent = Intent(this@ActivityAnimActivity, ActivityAnim2Activity::class.java)
            startActivity(intent)
            //传入动画文件，必须在startActivity、finish后立即执行不能延迟
            //第一个参数：打开的Activity要执行的动画
            //第二个参数：关闭的Activity要执行的动画
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
        }
    }
}